package util;

import java.util.ArrayList;
import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.QueryResultList;

import entity.ImageItem;

public class ItemHelper {
    
    /***
     * Get `number` ImageItem-s from the datastore sorted by rating.
     * Optionally filters by a single tag.  
     * @param offset
     * @param number
     * @param tag, optional
     * @return
     */
    public static List<ImageItem> getLadder(int offset, int number, String tag)
    {
        Query query = new Query("ImageItem");
        //filter by tag?
        if(tag != null)
        {
            query.addFilter(tag + "Tag", Query.FilterOperator.EQUAL, true);
        }
        
        query.addSort("rating", SortDirection.DESCENDING);
        DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
        PreparedQuery pq = ds.prepare(query);
        List<Entity> results = pq.asList(FetchOptions.Builder.withLimit(number).offset(offset));
        List<ImageItem> ret = new ArrayList<ImageItem>();
        for(Entity e: results)
        {
            ret.add(new ImageItem(e));
        }
        //System.out.println("Have " + ret.size());
        return ret;
    }
	
	public static List<ImageItem> getItemsForDuel(int numItems) {
		int numRandomItems = numItems / 2;
		int numNewItems = numItems - numRandomItems;

		DatastoreService ds = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("ImageItem");
		q.addSort("num_duels", SortDirection.ASCENDING);
		PreparedQuery pq = ds.prepare(q);
		List<Entity> duelingList = pq.asList(FetchOptions.Builder.withLimit(numNewItems));
		
		q = new Query("ImageItem");
		List<Entity> allEntities = ds.prepare(q).asList(FetchOptions.Builder.withDefaults());
		int totalEntities = allEntities.size();
		
		while (duelingList.size() < numItems) {
			int randomIndex = (int)(Math.random() * totalEntities);
			Entity randomItem = pq.asQueryResultList(FetchOptions.Builder.withLimit(numRandomItems).offset(randomIndex)).get(0);
			if (!duelingList.contains(randomItem)) {
				duelingList.add(randomItem);
			}
		}
			
		List<ImageItem> itemList = new ArrayList<ImageItem>();
	
		for (Entity e : duelingList) {
			itemList.add(new ImageItem(e)); 
		}
	
		return itemList;
	}
    
    /*
     * Helper method that calculates ELO adjustment, using a slightly modified version of the FIDE formula
     * @param r1, rating of player1
     * @param r2, rating of player2
     * @param winner, true is player1 won; false if player2 won
     */
    public static int calcEloAdjustment(int r1, int r2, boolean winner) {
        int max_adj = 32;
        double multiplier = 1.0;
        if (winner)
            multiplier = (1/(1+Math.pow(10.0,(r1-r2)/400.0)));
        else
            multiplier = (1/(1+Math.pow(10.0,(r2-r1)/400.0)));
        int adjustment = (int)Math.round(max_adj * multiplier);
        return adjustment;
    }
    
    /* adjust the stats of two items given a winner */
    public static void adjustStats(ImageItem i1, ImageItem i2, boolean winner) {
    	int r1 = i1.getRating();
    	int r2 = i2.getRating();
    	int adjustment = calcEloAdjustment(r1, r2, winner);
    	if (winner) {
    		i1.setRating(r1+adjustment);
    		i1.setRating(r2-adjustment);
    	} else {
    		i1.setRating(r1-adjustment);
    		i2.setRating(r2+adjustment);
    	}
    	i1.setNum_duels(i1.getNum_duels()+1);
    	i2.setNum_duels(i2.getNum_duels()+1);
    	
    	i1.save();
    	i2.save();
    }
    
    /* given the resulting ordering of a battle, commit changes to respective ratings */
    public static void resolveDuel(List<String> itemList) {
    	adjustStats(ImageItem.get(itemList.get(0)), ImageItem.get(itemList.get(1)), true);
    	adjustStats(ImageItem.get(itemList.get(0)), ImageItem.get(itemList.get(2)), true);
    	adjustStats(ImageItem.get(itemList.get(0)), ImageItem.get(itemList.get(3)), true);
    	adjustStats(ImageItem.get(itemList.get(1)), ImageItem.get(itemList.get(2)), true);
    	adjustStats(ImageItem.get(itemList.get(1)), ImageItem.get(itemList.get(3)), true);
    	adjustStats(ImageItem.get(itemList.get(2)), ImageItem.get(itemList.get(3)), true);
    }
    
	public static ImageItem[] loadStatic() {
		int num = 6;
		ImageItem[] itemArray = new ImageItem[num];
		String[][] itemData = new String[num][6];
        itemData[0] = new String[] {"http://i.imgur.com/0kc0s.jpg", "Baby leopards?", "", "cat|pets", "1729", "18"};
        itemData[1] = new String[] {"http://i.imgur.com/7p0ko.jpg", "Stray porch kitty", "", "cat|pets", "1568", "11"};
        itemData[2] = new String[] {"http://i.imgur.com/sN94a.gif", "Kitten Roomba", "", "cat|pets", "1231", "5"};
        itemData[3] = new String[] {"http://i.imgur.com/wwwkz.gif", "Crack-shot? More like cat-shot.", "", "cat|pets|human", "1761", "5"};
        itemData[4] = new String[] {"http://i.imgur.com/WgV0q.jpg", "Struttin'", "", "cat", "1366", "5"};
        itemData[5] = new String[] {"http://i.imgur.com/uNNkL.jpg", "Ebay Stray Kitten", "", "cat", "1483", "8"};
/*        itemData[6] = new String[] {"http://i.imgur.com/IHQlz.jpg", "Captain Cat", "", "cat", "1718", "9"};
        itemData[7] = new String[] {"http://i.imgur.com/Eiixf.jpg", "Downcast Fuzzball", "", "dog", "1253", "18"};
        itemData[8] = new String[] {"http://i.imgur.com/PDxH5.jpg", "Drink Up", "", "cat|pets", "1435", "11"};
        itemData[9] = new String[] {"http://i.imgur.com/IraC6.jpg", "Blissful comfort", "", "cat", "1336", "13"};
        itemData[10] = new String[] {"http://i.imgur.com/sQgYx.jpg", "I sleep how I wants", "", "cat", "1409", "18"};
        itemData[11] = new String[] {"http://i.imgur.com/Gw5e9.jpg", "Jazz Hand Cat", "", "cat", "1516", "17"};
        itemData[12] = new String[] {"http://i.imgur.com/Y1mkX.jpg", "Bathtime sadness", "", "cat", "1644", "2"};
        itemData[13] = new String[] {"http://i.imgur.com/kSkux.jpg", "She fought the shoe and the shoe won", "", "cat", "1434", "19"};
       %%%itemData[14] = new String[] {"http://i.imgur.com/OtKiks.jpg", "Yawn", "", "cat", "1716", "4"};
        %%%itemData[15] = new String[] {"http://i.imgur.com/SbZpls.jpg", "Please?", "", "pets|cat", "1780", "3"};
        itemData[16] = new String[] {"http://i.imgur.com/xSYVy.jpg", "aTitle16", "", "", "1504", "20"};
        %%%itemData[17] = new String[] {"http://i.imgur.com/Up1dCs.jpg", "aTitle17", "", "", "1370", "10"};
        %%%itemData[18] = new String[] {"http://i.imgur.com/NHddAs.jpg", "aTitle18", "", "", "1737", "2"};
        %%%itemData[19] = new String[] {"http://i.imgur.com/y30Sys.jpg", "aTitle19", "", "", "1439", "9"};
        %%%itemData[20] = new String[] {"http://i.imgur.com/mnxyes.jpg", "aTitle20", "", "", "1515", "5"};
        itemData[21] = new String[] {"http://i.imgur.com/bYnZA.jpg", "Family Cat Photography", "", "cat|pets", "1755", "20"};
        %%%itemData[22] = new String[] {"http://i.imgur.com/OUnHUs.jpg", "Let me tell you something...", "", "dog|cat|pets", "1658", "15"};
        %%%itemData[23] = new String[] {"http://i.imgur.com/08apds.jpg", "aTitle23", "", "", "1320", "16"};
        %%%itemData[24] = new String[] {"http://i.imgur.com/JSeD0s.jpg", "aTitle24", "", "", "1240", "20"};
        itemData[25] = new String[] {"http://i.imgur.com/kKNAN.jpg", "Here's looking at you, kid", "", "cat|pets", "1702", "18"};
        %%%itemData[26] = new String[] {"http://i.imgur.com/sjxWT.jpg", "I'm a pine cone", "", "wildlife", "1442", "8"};
        %%%itemData[27] = new String[] {"http://i.imgur.com/6EdoVs.jpg", "aTitle27", "", "", "1659", "20"};
        itemData[28] = new String[] {"http://i.imgur.com/zXt2Hs.jpg", "Goldilocks and the three ... Pandas?", "", "humans|wildlife", "1210", "18"};
        %%%itemData[29] = new String[] {"http://i.imgur.com/pfSSMs.jpg", "aTitle29", "", "", "1699", "17"};
        %%%itemData[30] = new String[] {"http://i.imgur.com/HYdEhs.jpg", "aTitle30", "", "", "1384", "16"};
        %%%itemData[31] = new String[] {"http://i.imgur.com/nrkrMs.jpg", "aTitle31", "", "", "1401", "4"};
        itemData[32] = new String[] {"http://i.imgur.com/pVMWE.jpg", "I've fallen and I can't get up!", "", "wildlife", "1321", "4"};
        itemData[33] = new String[] {"http://i.imgur.com/U3Txt.jpg", "What's up doc?", "", "wildlife", "1474", "3"};
        itemData[34] = new String[] {"http://i.imgur.com/hz90a.jpg", "Tomatoes give you peace", "", "cat|pets", "1581", "3"};
        %%%itemData[35] = new String[] {"http://i.imgur.com/c7pwCs.jpg", "aTitle35", "", "", "1673", "13"};
        %%%itemData[36] = new String[] {"http://i.imgur.com/EQEDis.jpg", "aTitle36", "", "", "1663", "2"};
        itemData[37] = new String[] {"http://i.imgur.com/x6tXk.jpg", "To the moon!", "", "wildlife", "1370", "9"};
        %%%itemData[38] = new String[] {"http://i.imgur.com/vfJJvs.jpg", "aTitle38", "", "", "1723", "16"};
        %%%itemData[39] = new String[] {"http://i.imgur.com/Hk2Uss.jpg", "aTitle39", "", "", "1470", "9"};
        %%%itemData[40] = new String[] {"http://i.imgur.com/njmO5s.jpg", "aTitle40", "", "", "1523", "4"};
        itemData[41] = new String[] {"http://i.imgur.com/EkPir.jpg", "Why does this egg feel so large?", "", "dog|bird", "1357", "20"};
        itemData[42] = new String[] {"http://i.imgur.com/Umz3e.jpg", "Feeding the Managerie", "", "pets", "1493", "5"};
        itemData[43] = new String[] {"http://i.imgur.com/YTIzZ.jpg", "My teddy pig...", "", "wildlife", "1319", "2"};
        itemData[44] = new String[] {"http://i.imgur.com/bitj7.jpg", "Best buds", "", "pets|bird", "1309", "8"};
        itemData[45] = new String[] {"http://i.imgur.com/0aisUs.jpg", "aTitle45", "", "", "1200", "3"};
        itemData[46] = new String[] {"http://i.imgur.com/atNvas.jpg", "aTitle46", "", "", "1692", "9"};
        itemData[47] = new String[] {"http://i.imgur.com/LWg3Hs.jpg", "aTitle47", "", "", "1353", "13"};
        itemData[48] = new String[] {"http://i.imgur.com/051pE.jpg", "Aww...", "", "cat|pets", "1233", "16"};
        itemData[49] = new String[] {"http://i.imgur.com/09C5G.jpg", "Battle helmets...on!", "", "wildlife", "1238", "1"};
        itemData[50] = new String[] {"http://i.imgur.com/09QN8.gif", "Mine!", "", "cat|pets", "1251", "13"};
        itemData[51] = new String[] {"http://i.imgur.com/0lkKH.jpg", "A comic!!!!", "", "", "1385", "13"};
        itemData[52] = new String[] {"http://i.imgur.com/0Oib3.gif", "Mirror mirror", "", "human", "1312", "4"};
        itemData[53] = new String[] {"http://i.imgur.com/0sAOA.jpg", "Bwahahaha!!!", "", "", "1509", "20"};
        itemData[54] = new String[] {"http://i.imgur.com/1j5WR.jpg", "Yin-yang", "", "cat|pets", "1690", "3"};
        itemData[55] = new String[] {"http://i.imgur.com/lyEMJ.jpg", "What'ya looking at?", "", "cat|pets", "1372", "16"};
        itemData[56] = new String[] {"http://i.imgur.com/2SUON.jpg", "Yo, what's up?", "", "dog|pets", "1541", "12"};
        itemData[57] = new String[] {"http://i.imgur.com/2tGUi.jpg", "Please get me out of this dirty place!", "", "cat|pets", "1415", "5"};
        itemData[58] = new String[] {"http://i.imgur.com/2zGI0.jpg", "The bleached managerie and its newest member!", "", "cat|dog|pets", "1510", "16"};
        itemData[59] = new String[] {"http://i.imgur.com/3rrs5.jpg", "We're going to need more cats...", "", "cat|pets", "1288", "20"};
        itemData[60] = new String[] {"http://i.imgur.com/3SCdT.jpg", "My kitty to hold", "", "human|cat|pets", "1723", "6"};
        itemData[61] = new String[] {"http://i.imgur.com/5aYB8.jpg", "Great sayings", "", "", "1633", "15"};
        itemData[62] = new String[] {"http://i.imgur.com/7G2lS.jpg", "Bear return", "", "pets", "1254", "7"};
        itemData[63] = new String[] {"http://i.imgur.com/7NHEL.png", "He doesn't know...", "", "cat|wildlife", "1485", "3"};
        itemData[64] = new String[] {"http://i.imgur.com/9nhJ8.jpg", "Let me at 'em!", "", "cat|dog|pets", "1305", "2"};
        itemData[65] = new String[] {"http://i.imgur.com/9Uv0R.jpg", "What is this all about?", "", "dog|pets", "1608", "4"};
        itemData[66] = new String[] {"http://i.imgur.com/a5mr6.jpg", "Keepsake turtle", "", "pets", "1521", "12"};
        itemData[67] = new String[] {"http://i.imgur.com/aLCZW.jpg", "Shh...", "", "cat|pets", "1418", "9"};
        itemData[68] = new String[] {"http://i.imgur.com/aLkUc.jpg", "I'm Super Bird!", "", "wildlife|bird", "1372", "17"};
        itemData[69] = new String[] {"http://i.imgur.com/AMSWF.jpg", "All heil the kitty!", "", "cat|pets", "1732", "12"};
        itemData[70] = new String[] {"http://i.imgur.com/ANCb9.jpg", "Pileup on the kitty freeway", "", "cat|pets", "1278", "10"};
        itemData[71] = new String[] {"http://i.imgur.com/bd8JN.jpg", "Here's your cat", "", "wildlife|cat", "1553", "4"};
        itemData[72] = new String[] {"http://i.imgur.com/BpBkl.jpg", "Tail-tagging along", "", "cat|dog|pets", "1446", "19"};
        itemData[73] = new String[] {"http://i.imgur.com/ciqY0.jpg", "The Express Bus", "", "wildlife", "1699", "3"};
        itemData[74] = new String[] {"http://i.imgur.com/czAFG.jpg", "Kitty kitty", "", "cat", "1739", "14"};
        itemData[75] = new String[] {"http://i.imgur.com/D3o72.jpg", "Model cat", "", "cat|pets", "1649", "1"};
        itemData[76] = new String[] {"http://i.imgur.com/D5pW9.png", "Here's how it's going to go down...", "", "dog|pets", "1658", "17"};
        itemData[77] = new String[] {"http://i.imgur.com/eKJnv.jpg", "Top hat cat", "", "cat|pets", "1330", "12"};
        itemData[78] = new String[] {"http://i.imgur.com/eSDDU.jpg", "Attack of the killer bubble cat!", "", "cat|human", "1413", "1"};
        itemData[79] = new String[] {"http://i.imgur.com/F43zN.jpg", "Teddy bear for all ages", "", "cat|pets", "1703", "3"};
        itemData[80] = new String[] {"http://i.imgur.com/fcfF7.jpg", "Bunny Yoga", "", "pets", "1743", "3"};
        itemData[81] = new String[] {"http://i.imgur.com/fI2gl.jpg", "Yeah, leaf!", "", "wildlife", "1759", "2"};
        itemData[82] = new String[] {"http://i.imgur.com/fxvrU.jpg", "This is a flower, right?", "", "wildlife", "1372", "3"};
        itemData[83] = new String[] {"http://i.imgur.com/GbvT7.jpg", "High one!", "", "cat|human|pets", "1471", "13"};
        itemData[84] = new String[] {"http://i.imgur.com/GLiEo.jpg", "We shouldn't have eaten so much....", "", "wildlife", "1622", "14"};
        itemData[85] = new String[] {"http://i.imgur.com/GzrV8.jpg", "The AT&T Free Cat Offer!", "", "cat|pets", "1620", "6"};
        itemData[86] = new String[] {"http://i.imgur.com/rBdZV.jpg", "Honey, I blew up the Cat!", "", "cat|pets", "1264", "12"};
        itemData[87] = new String[] {"http://i.imgur.com/HAgja.jpg", "The Cat Rug", "", "cat|pets", "1587", "14"};
        itemData[88] = new String[] {"http://i.imgur.com/I5vH4.jpg", "Calvin and Hobbes - Wind", "", "", "1562", "17"};
        itemData[89] = new String[] {"http://i.imgur.com/IaKxd.jpg", "Tasting the meal", "", "cat|dog|pets", "1672", "13"};
        itemData[90] = new String[] {"http://i.imgur.com/iKy1W.gif", "Cat Massage", "", "cat|pets", "1460", "15"};
        itemData[91] = new String[] {"http://i.imgur.com/JqVXz.gif", "The cool way to put a hat on", "", "human", "1354", "14"};
        itemData[92] = new String[] {"http://i.imgur.com/jVpOB.jpg", "I'm fat, but I'm cold", "", "cat|dog", "1226", "13"};
        itemData[93] = new String[] {"http://i.imgur.com/jzGIv.jpg", "Next Shrek film: Pig in Boots", "", "wildlife", "1406", "14"};
        itemData[94] = new String[] {"http://i.imgur.com/Kh6Vs.jpg", "Missing doll, what missing doll?", "", "cat|pets", "1307", "10"};
        itemData[95] = new String[] {"http://i.imgur.com/KIz7s.jpg", "Get away from my kid!", "", "cat|pets", "1275", "1"};
        itemData[96] = new String[] {"http://i.imgur.com/kOfiH.jpg", "Ducks on the duck boat", "", "wildlife", "1300", "19"};
        itemData[97] = new String[] {"http://i.imgur.com/LJe2J.jpg", "Another one of Bambi's friends", "", "wildlife|cats", "1358", "15"};
        itemData[98] = new String[] {"http://i.imgur.com/lZJ8p.jpg", "Oh, why did I look down?", "", "cat|pets", "1511", "17"};
        itemData[99] = new String[] {"http://i.imgur.com/lZUw9.jpg", "Mr. Whiskers", "", "cat|pets", "1404", "10"};
        itemData[100] = new String[] {"http://i.imgur.com/mOC0k.jpg", "Cat on Deck", "", "cat", "1502", "5"};
        itemData[101] = new String[] {"http://i.imgur.com/MTm67.jpg", "Chillaxing...", "", "cat|pets", "1727", "20"};
        itemData[102] = new String[] {"http://i.imgur.com/MuVOB.jpg", "Flat Cat", "", "cat|pets", "1336", "7"};
        itemData[103] = new String[] {"http://i.imgur.com/N6dS3.jpg", "Dude, you're supposed to catch me!", "", "wildlife", "1628", "1"};
        itemData[104] = new String[] {"http://i.imgur.com/n7Xfn.jpg", "Cuddling Cutie Cats", "", "cat|pets", "1634", "1"};
        itemData[105] = new String[] {"http://i.imgur.com/nNuuW.jpg", "Siamese Triplet Cats", "", "cat|pets", "1349", "18"};
        itemData[106] = new String[] {"http://i.imgur.com/NUDtZ.jpg", "Nap Time", "", "cat|pets", "1791", "2"};
        itemData[107] = new String[] {"http://i.imgur.com/NuV1l.jpg", "Save yourself...", "", "cat", "1577", "7"};
        itemData[108] = new String[] {"http://i.imgur.com/oAj7Y.jpg", "I'm the next elmo", "", "cat|pets", "1283", "9"};
        itemData[109] = new String[] {"http://i.imgur.com/ODvWU.jpg", "I'm trapped!", "", "human|cat", "1342", "10"};
        itemData[110] = new String[] {"http://i.imgur.com/pN0bf.jpg", "Why He took the 7th day off...", "", "", "1660", "5"};
        itemData[111] = new String[] {"http://i.imgur.com/pTjuC.jpg", "Really Flat Cat", "", "cat|pets", "1753", "13"};
        itemData[112] = new String[] {"http://i.imgur.com/pUBTP.png", "Felled Over", "", "pets", "1628", "13"};
        itemData[113] = new String[] {"http://i.imgur.com/PPHnY.jpg", "Nighty Night", "", "cat|pets", "1301", "10"};
        itemData[114] = new String[] {"http://i.imgur.com/qcbwG.jpg", "March of the Pandas", "", "wildlife", "1638", "8"};
        itemData[115] = new String[] {"http://i.imgur.com/r1SZ1.jpg", "Cuddle with me", "", "cat|pets", "1275", "14"};
        itemData[116] = new String[] {"http://i.imgur.com/R27b8.jpg", "Tongue", "", "cat|pets", "1223", "13"};
        itemData[117] = new String[] {"http://i.imgur.com/rTDty.jpg", "A bowl of kitty", "", "cat|pets", "1488", "11"};
        itemData[118] = new String[] {"http://i.imgur.com/SjztC.jpg", "A dog", "", "dog", "1678", "8"};
        itemData[119] = new String[] {"http://i.imgur.com/Suqee.jpg", "Basket Case Cats", "", "cat|pets", "1509", "9"};
        itemData[120] = new String[] {"http://i.imgur.com/taoBK.jpg", "Cat Supplication", "", "cat|pets", "1356", "20"};
        itemData[121] = new String[] {"http://i.imgur.com/tDhCW.jpg", "I dont' care if it's just a head, it's my doll!", "", "cat|pets", "1251", "16"};
        itemData[122] = new String[] {"http://i.imgur.com/TeX1P.jpg", "Bubble Bathtime", "", "pets", "1461", "1"};
        itemData[123] = new String[] {"http://i.imgur.com/TNf3B.jpg", "So relaxed", "", "cat|pets", "1486", "17"};
        itemData[124] = new String[] {"http://i.imgur.com/to2j7.jpg", "The cat ladder", "", "cat|pets", "1545", "17"};
        itemData[125] = new String[] {"http://i.imgur.com/UG2lU.jpg", "I love you mom", "", "cat|pets", "1476", "3"};
        itemData[126] = new String[] {"http://i.imgur.com/uklWq.jpg", "Sunlight! Finally!", "", "wildlife", "1785", "14"};
        itemData[127] = new String[] {"http://i.imgur.com/Uq5uB.jpg", "Cathernet", "", "cat|pets", "1683", "13"};
        itemData[128] = new String[] {"http://i.imgur.com/wkRT0.jpg", "Oh Yeah!", "", "cat|human|pets", "1567", "15"};
        itemData[129] = new String[] {"http://i.imgur.com/V7XM3.jpg", "There's always the odd one out", "", "cat|pets", "1663", "6"};
        itemData[130] = new String[] {"http://i.imgur.com/v8bvW.jpg", "Duck gym class", "", "wildlife", "1674", "12"};
        itemData[131] = new String[] {"http://i.imgur.com/VFHC6.jpg", "nom nom nom", "", "wildlife", "1638", "19"};
        itemData[132] = new String[] {"http://i.imgur.com/VtkMC.jpg", "A Dog and his kitty", "", "dog|cat|pets", "1664", "18"};
        itemData[133] = new String[] {"http://i.imgur.com/Wdh6P.jpg", "Chocolate-claw cat", "", "cat|pets", "1645", "14"};
        itemData[134] = new String[] {"http://i.imgur.com/we1EG.jpg", "Get him off of me!", "", "cat|pets", "1313", "2"};
        itemData[135] = new String[] {"http://i.imgur.com/wEYKU.gif", "Alligator! RUN!!", "", "dog", "1687", "8"};
        itemData[136] = new String[] {"http://i.imgur.com/X8JHL.gif", "Window Acrobatics", "", "cat|pets", "1650", "12"};
        itemData[137] = new String[] {"http://i.imgur.com/xOacj.jpg", "Corn: the new nut", "", "wildlife", "1332", "9"};
        itemData[138] = new String[] {"http://i.imgur.com/XYb5J.jpg", "Hot Dog Wrap", "", "dog|pets", "1321", "16"};
        itemData[139] = new String[] {"http://i.imgur.com/xznY5.jpg", "Bah!", "", "cat", "1654", "17"};
        itemData[140] = new String[] {"http://i.imgur.com/Y0PCI.jpg", "Easter Bonnet Cat", "", "cat", "1572", "13"};
        itemData[141] = new String[] {"http://i.imgur.com/y6T43.gif", "Time to dance!", "", "cat|pets", "1484", "18"};
        itemData[142] = new String[] {"http://i.imgur.com/ODCxu.jpg", "The Wolf Inside", "", "dog|wildlife", "1314", "2"};
        itemData[143] = new String[] {"http://i.imgur.com/y2tBm.jpg", "The Cat Cave", "", "cat|pets", "1796", "11"};
        itemData[144] = new String[] {"http://i.imgur.com/zduRm.jpg", "1st Rule of the Buddy System", "", "cat|pets", "1505", "15"};
        itemData[145] = new String[] {"http://i.imgur.com/LkwBy.jpg", "Jekyll and Hyde", "", "cat|pets", "1633", "3"};
        itemData[146] = new String[] {"http://i.imgur.com/F6BrU.jpg", "Bosely Medical Center for Ear Restoration", "", "dog|pets", "1251", "20"};
        itemData[147] = new String[] {"http://i.imgur.com/ALodn.jpg", "Yawn....", "", "cat", "1335", "6"};
        itemData[148] = new String[] {"http://i.imgur.com/gNIB6.jpg", "Yum yum", "", "cat|pets", "1745", "19"};
        itemData[149] = new String[] {"http://i.imgur.com/uo69N.jpg", "Coming soon to a tree near you...", "", "wildlife", "1253", "7"};
        itemData[150] = new String[] {"http://i.imgur.com/3j3rU.jpg", "I've heard of computer bugs before, but...", "", "cat|pets", "1495", "1"};
        itemData[151] = new String[] {"http://i.imgur.com/MpqfR.jpg", "Cat as a Hat", "", "cat|pets|human", "1317", "18"};
        itemData[152] = new String[] {"http://i.imgur.com/wAs6h.jpg", "Cat on Dog", "", "cat|dog|pets", "1462", "5"};
        itemData[153] = new String[] {"http://i.imgur.com/uYaqG.jpg", "Can someone color me in?", "", "cat|pets", "1436", "20"};
        itemData[154] = new String[] {"http://i.imgur.com/bcv2X.jpg", "The Dog as Zerg", "", "dog|pets", "1290", "7"};
        itemData[155] = new String[] {"http://i.imgur.com/0TNbK.jpg", "The Sleeping Monks", "", "pets", "1584", "6"};
        itemData[156] = new String[] {"http://i.imgur.com/rV1vk.jpg", "A flower for you, pal", "", "dog|pets", "1663", "18"};
        itemData[157] = new String[] {"http://i.imgur.com/OBEL8.jpg", "I bite my fingernails when I sleep", "", "cat|pets", "1502", "7"};
        itemData[158] = new String[] {"http://i.imgur.com/xa7nU.jpg", "Pick of the Litter", "", "dog|pets", "1464", "6"};
        itemData[159] = new String[] {"http://i.imgur.com/6VV9m.jpg", "The new Kermit the Dog", "", "dog|pets", "1326", "20"};
        itemData[160] = new String[] {"http://i.imgur.com/WPEJd.jpg", "Come inside and think about what you've done...", "", "dog|pets", "1646", "9"};
        itemData[161] = new String[] {"http://i.imgur.com/x91IE.jpg", "Ice Weasel", "", "wildlife", "1315", "2"};
        itemData[162] = new String[] {"http://i.imgur.com/myDMM.jpg", "The A Team", "", "pets|cat", "1563", "5"};
        itemData[163] = new String[] {"http://i.imgur.com/sa6LM.jpg", "I was jumping...", "", "dog|pets", "1597", "4"};
        itemData[164] = new String[] {"http://i.imgur.com/BgrPp.png", "Synchronized Napping", "", "cat|pets", "1739", "14"};
        itemData[165] = new String[] {"http://i.imgur.com/MJXpQ.jpg", "I'm bored...", "", "cat", "1568", "11"};
        itemData[166] = new String[] {"http://i.imgur.com/talor.jpg", "Boyfriend and Girlfriend", "", "cat|dog|pets", "1395", "14"};
        itemData[167] = new String[] {"http://i.imgur.com/NPLxt.jpg", "Boo!", "", "cat", "1698", "4"};
        itemData[168] = new String[] {"http://i.imgur.com/nEIDM.gif", "Delayed Reaction Button", "", "pets", "1722", "8"};
        itemData[169] = new String[] {"http://i.imgur.com/TNrTd.jpg", "Being Close to Your Food", "", "dog|pets", "1354", "1"};
        itemData[170] = new String[] {"http://i.imgur.com/71Fv6.jpg", "Catch of the day, meh", "", "human|dog", "1778", "14"};
        itemData[171] = new String[] {"http://i.imgur.com/PMnOv.jpg", "Kleenex Cat", "", "cat|pets", "1605", "4"};
        itemData[172] = new String[] {"http://i.imgur.com/Og2lw.jpg", "The Leopard Cat", "", "cat", "1377", "11"};
        itemData[173] = new String[] {"http://i.imgur.com/Mpda0.jpg", "Staring Contest", "", "cat|pets", "1375", "1"};
        itemData[174] = new String[] {"http://i.imgur.com/eHSL2.jpg", "Doggy Swing Set", "", "dog|pets", "1359", "10"};
        itemData[175] = new String[] {"http://i.imgur.com/0gdOi.jpg", "Here's the sun, little kitty", "", "cat|pets", "1214", "9"};
        itemData[176] = new String[] {"http://i.imgur.com/KBhbT.jpg", "My doll is in there", "", "cat|pets", "1548", "15"};
        itemData[177] = new String[] {"http://i.imgur.com/R85qs.jpg", "Elephant Cast", "", "human|wildlife", "1268", "15"};
        itemData[178] = new String[] {"http://i.imgur.com/zWCi8.jpg", "Whoa", "", "cat", "1742", "8"};
        itemData[179] = new String[] {"http://i.imgur.com/HVSbh.jpg", "Kitty Wristwatch", "", "cat|human|pets", "1729", "8"};
*/
		int kk = 0;
		for (kk = 0; kk < itemData.length; kk++) {
			itemArray[kk] = new ImageItem(
					itemData[kk][0],
					itemData[kk][1],
					itemData[kk][2],
					(itemData[kk][3].contains("cat")) ? true : false,
					(itemData[kk][3].contains("dog")) ? true : false,
					(itemData[kk][3].contains("bird")) ? true : false,
					(itemData[kk][3].contains("pets")) ? true : false,
					(itemData[kk][3].contains("wildlife")) ? true : false,
					(itemData[kk][3].contains("human")) ? true : false,					
					(itemData[kk][4] == "") ? 1500 : Integer.parseInt(itemData[kk][4]),
					(itemData[kk][5] == "") ? 1 : Integer.parseInt(itemData[kk][5]),
					"batch_loader");
			itemArray[kk].save();
			System.out.println(itemArray[kk].toString());
    	}
		return itemArray;
	}
}